package cn.edu.swu.zwy;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Comparator;

public class Main {
    public static void main(String[] args) throws Exception{
        String img1="http://www.swu.edu.cn/xywh/zhanbanzongban/01.jpg";
        String img2="http://www.swu.edu.cn/xywh/xiaoshi01/01.jpg";
        String img3="http://www.swu.edu.cn/xywh/xiaoshi06/01.jpg";
        String img4="http://www.swu.edu.cn/xywh/xiaoshi08/01.jpg";
        String img5="http://www.swu.edu.cn/xywh/xiaoshi10/01.jpg";
        //String一个数组存储img
        String[] images=new String[]{img1,img2,img3,img4,img5};
        String target="C:/images/cn.edu.swu/zwy";
        download(images,target);
        sorting(images);
        System.out.println("创建成功!");

    }

    public static  void download(String[] images, String target){
        try {
            //创建target地址对应所需文件夹
            File file = new File(target);
            file.mkdirs();
            //for循环每张照片的下载地址
            for (int i = 0; i <images.length; i++) {
                //new一个URL对象
                URL url=new URL(images[i]);
                HttpURLConnection conn=(HttpURLConnection)url.openConnection();
                conn.setRequestMethod("GET");
                conn.setConnectTimeout(5*1000);
                //输入流
                InputStream is=conn.getInputStream();
                byte[] data=readInputStream(is);
                File imagedictory = new File("C:/images/cn.edu.swu/zwy/"+(i+1));
                imagedictory.mkdirs();
                //new文件对象用来保存图片
                File imagefile=null;
                imagefile = new File("C:/images/cn.edu.swu/zwy/" + (i + 1) +"/"+(i+1)+".jpg");
                //输出流
                FileOutputStream os = new FileOutputStream(imagefile);
                os.write(data);
                os.close();//关闭输出流

                //os.close();//关闭输出流
            }
        }catch (ArrayIndexOutOfBoundsException e){
            e.printStackTrace();
        }catch (Exception e){
            e.printStackTrace();
        }
    }


    public static  byte[] readInputStream(InputStream is) throws IOException {
        ByteArrayOutputStream os=new ByteArrayOutputStream();
        byte[] buffer=new byte[1024];
        //每次读取的字符串长度，如果为-1，代表全部读取完毕
        int len=0;
        while ((len=is.read(buffer))!=-1){
            os.write(buffer,0,len);
        }
        is.close();
        return os.toByteArray();
    }

    public  static void sorting(String[] images) throws Exception {
        OutputStream os = new FileOutputStream("C:/images/images-sorted.txt", true);
        try {
            File targetfile = new File("C:/images/images-sorted.txt");
            targetfile.createNewFile();


            long[] size = new long[images.length];
            for (int i = 0; i < images.length; i++) {
                File file = new File("C:/images/cn.edu.swu/zwy/" + (i + 1) +"/"+(i+1) +".jpg");
                size[i] = file.length();
            }
            long temp;
            for (int i = 0; i < images.length - 1; i++) {
                for (int j = i + 1; j < images.length; j++) {
                    if (size[i] < size[j]) {
                        temp = size[i];
                        size[i] = size[j];
                        size[j] = temp;
                    }
                }

            }
            for (int i = 0; i < images.length; i++) {
                for (int j = 0; j < images.length; j++) {
                    File file = new File("C:/images/cn.edu.swu/zwy/" + (j + 1) +"/"+(j+1) +".jpg");

                    if (size[i] == file.length()) {
                        String s=String.valueOf(size[i]);
                        os.write(s.getBytes());
                        os.write(' ');
                        os.write(file.getAbsolutePath().getBytes());
                        os.write("\r\n".getBytes());
                    }
                }
            }

        } catch (ArrayIndexOutOfBoundsException e){
            e.printStackTrace();
        } catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                if(os!=null)    os.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
}

